import Vue from 'vue'
import Vuex from 'vuex'
//引入封装好的模块
import menu from './modules/menu'
import role from './modules/role'
import user from "./modules/user";
import cate from './modules/cate'
import specs from "./modules/specs";
import goods from './modules/goods'
import member from './modules/member'
import banner from './modules/pic'
import seckill from './modules/seckill'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    userinfo: sessionStorage.getItem('userinfo')? JSON.parse(sessionStorage.getItem('userinfo')) : null
  },
  getters: {
    getUser(state){
      return state.userinfo
    }
  },
  mutations: {
    //用于修改state的 userinfo
    REQ_USERINFO(state,payload){
      //如果有参数 且 参数为真 我们就可以把数据存储到会话存储中否则进行移除处理
      if(payload){
        state.userinfo = payload
        sessionStorage.setItem('userinfo',JSON.stringify(payload))
      }else{
        sessionStorage.removeItem('userinfo')
      }
      
    }
  },
  actions: {
    //发起行动
    changeUserInfoAction({commit},payload){
      commit('REQ_USERINFO',payload)
    }
  },
  modules: {
    menu,
    role,
    user,
    cate,
    specs,
    goods,
    member,
    banner,
    seckill
  }
})
